package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.edu.nuc.onlinestore.util.Carutil;
import cn.edu.nuc.onlinestore.util.GoodsUpdateutil;
import cn.edu.nuc.onlinestore.vo.Goods;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserCartFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;

	private JTextField selectText;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserCartFrame frame = new UserCartFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserCartFrame() {
		setTitle("购物车详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
//		setBounds(100, 100, 591, 377);
//		contentPane = new JPanel();
//		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//		setContentPane(contentPane);
//		contentPane.setLayout(null);
//		
		
		
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(￥)");
		model.addColumn("数量");
		model.addColumn("总价格(￥)");
		//model.addColumn("操作");
		
		
		File f=new File("D:\\Car\\");
		File[] f1=f.listFiles();
		
		for(int i=0;i<f1.length;i++){
			GoodsUpdateutil gu=new GoodsUpdateutil();
			Goods g=gu.ObjectUtil_a(f1[i]);
				
			model.addRow(new String[]{String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum()),String.valueOf(g.getNum()*g.getPrice())});
		}
		panel.setLayout(null);
	
		    
		    
		
		
//		model.addRow(new String[]{"1","水杯","15.00","2","30.00"});
//		model.addRow(new String[]{"5","农夫山泉","2.00","5","10"});
//		model.addRow(new String[]{"6","毛巾","9.90","1","9.90"});
//		table = new JTable( model );
//		table.setBounds(10, 38, 543, 184);
//		
//		JScrollPane pane = new JScrollPane( table );
//		
//		contentPane.add( table );
			
		JTable table = new JTable( model );
			
		JScrollPane pane = new JScrollPane( table );
		pane.setBounds(0, 0, 696, 341);
			
		panel.add(pane);
		contentPane.add(panel);	
		
		JLabel SumGoods = new JLabel("总商品数量: 8");
		SumGoods.setBounds(37, 30, 116, 15);
		contentPane.add(SumGoods);
		
		JLabel SumPrice = new JLabel("总金额: 49.90");
		SumPrice.setBounds(192, 30, 211, 15);
		contentPane.add(SumPrice);
		/**
		 * 结账
		 * 讲购物车所有商品对象传给服务端
		 * 服务端结算
		 * 减去库存
		 * 
		 */
		JButton button = new JButton("结账");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Carutil car=new Carutil();
				try {
					if(car.Buy()){
						model.setRowCount(0);
						File f=new File("D:\\Car\\");
						File[] f1=f.listFiles();
						for(int i=0;i<f1.length;i++){
							System.out.println(f1[i].delete());
						}
						JOptionPane.showMessageDialog(null, "结账完成", "alert", JOptionPane.ERROR_MESSAGE); 
						UserStore.cf.disable();
					}
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		button.setBounds(487, 26, 93, 23);
		contentPane.add(button);
		double sum=0;
		int rows = table.getRowCount();
		System.out.println(rows);
		for(int i=0;i<rows;i++){
			sum+= Double.valueOf((String)model.getValueAt(i, 4));
		}
		SumGoods.setText("总商品数"+String.valueOf(rows));
		SumPrice.setText("总金额数"+String.valueOf(sum));
	}

}
